package com.demo.web;

import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.entity.Role;
import com.demo.entity.RolePermission;
import com.demo.entity.UserRole;
import com.demo.service.IRolePermissionService;
import com.demo.service.IRoleService;
import com.demo.vo.RolePermissionUnion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/role")
public class RoleController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IRoleService comService;
    @Autowired
    private IRolePermissionService dataService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Integer> create(String permissions, String name, String remark) {
        Role role = new Role();
        role.setName(name);
        role.setDescription(remark);
        int newId = comService.insert(role);
        String[] ar = permissions.split(",");
        List<RolePermission> rp = new ArrayList<RolePermission>();
        for (String item : ar) {
            RolePermission rp_item = new RolePermission();
            rp_item.setPermissionId(Integer.valueOf(item));
            rp_item.setRoleId(role.getId());
            rp.add(rp_item);
        }
        int li = dataService.addBatch(rp);
        BaseResult<Integer> res = new BaseResult<Integer>(true, li);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(Role arg) {
        arg.setStatus("0");
        //todo：
        UserRole ur = new UserRole();
        ur.setRoleId(arg.getId());
        int res = comService.queryByRoleId(ur);
        if (res > 0) {
            return new BaseResult(false, "该角色下关联用户数据，禁止删除!", 403);
        }
        int service_res = comService.updateByPrimaryKeySelective(arg);
        int del_res = dataService.deleteByRoleId(String.valueOf(arg.getId()));//上
        return new BaseResult(true, service_res);
    }

    /**
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(String permissions, String name, String remark, String id) {
        Role role = new Role();
        role.setRoleName(name);
        role.setDescription(remark);
        role.setId(Integer.valueOf(id));
        int newId = comService.updateByPrimaryKeySelective(role);
        int del_res = dataService.deleteByRoleId(id);

        String[] ar = permissions.split(",");
        List<RolePermission> rp = new ArrayList<RolePermission>();
        for (String item : ar) {
            RolePermission rp_item = new RolePermission();
            rp_item.setPermissionId(Integer.valueOf(item));
            rp_item.setRoleId(role.getId());
            rp.add(rp_item);
        }
        int li = dataService.addBatch(rp);
        BaseResult<Integer> res = new BaseResult<Integer>(true, li);
        return res;
    }

//    @ResponseBody
//    @RequestMapping(value = "/query", method = RequestMethod.GET)
//    public ListResult query(Role arg) {
//        List<Com> list = comService.queryByPage(arg);
//        int total = comService.queryCount(arg);
//        arg.getPage();
//        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
//        System.out.println("Size:" + arg.getSize());
//        System.out.println("getSortField:" + arg.getSortField());
//        ListResult res = new ListResult(true, list);
//        res.setPage(page);
//        return res;
//    }

    /***
     * 分页查询
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query() {
        List<RolePermissionUnion> list = comService.queryAllRoleWithPermission();
        ListResult res = new ListResult(true, list);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ListResult list() {
        List<Role> list = comService.getRoleList(null);
        ListResult res = new ListResult(true, list);
        return res;
    }
}
